﻿@page "/approve"
@attribute [Authorize(IdentityPermissions.Users.Default)]
@using System.ComponentModel
@using AntDesign.TableModels
@using ApproveFlowEngine.Approve
@using ApproveFlowEngine.Approve.DTO
@using ApproveFlowEngine.Enums.Core
@using Volo.Abp.Application.Dtos
@using Volo.Abp.Identity
@inject IApproveAppService _iApproveAppService

<h3>Approve</h3>

<Approve_Create OnCallBack="BindData"></Approve_Create>
<ApproveSetting visible="_visible" WorkFlow="_workFlowDto" OnCallBack="BindData"></ApproveSetting>
<Table TItem="WorkFlowDto" DataSource="@_pageData.Items" OnRowClick="OnRowClick">
    <PropertyColumn Property="c=>c.FlowName"></PropertyColumn>
    <PropertyColumn Property="c=>c.FlowNo"></PropertyColumn>
    <PropertyColumn Property="c=>c.FlowStatus"></PropertyColumn>
    <PropertyColumn Property="c=>c.ApproveType">
        <TitleTemplate>
            <span> <Icon Type="environment" /> Address </span>
        </TitleTemplate>
    </PropertyColumn>
    <PropertyColumn Property="c=>c.NodeTree">
        @foreach (var Node in context.Nodes)
        {
            var color = Node.Children.Count > 5 ? "geekblue" : "green";
            if (Node.NodeType is NodeType.Simple)
            {
                color = "volcano";
            }
            <Tag Color="@color">@Node.NodeType</Tag>
        }
    </PropertyColumn>
    <ActionColumn Title="Action">
        <Space Size=@("middle")>
            @if (context.FlowStatus < ApproveFlowStatus.Activity)
            {
                <SpaceItem>
                    <Button OnClick="()=>FlowStart(context.Id)">Start</Button>
                </SpaceItem>
            }
           
            <SpaceItem>
                <Button OnClick="() => { _visible = true; _workFlowDto = context; }">Setting</Button>
            </SpaceItem>
            <SpaceItem>
                <Button OnClick="() => Delete(context.Id)">Delete</Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@code {


    private bool _visible = false;
    private PagedResultDto<WorkFlowDto> _pageData = new PagedResultDto<WorkFlowDto>();
    private PagedResultRequestDto _pageRequest = new PagedResultRequestDto() { SkipCount = 0, MaxResultCount = 10 };
    private WorkFlowDto _workFlowDto = new WorkFlowDto();

    protected override async Task OnInitializedAsync()

    {
        await BindData();
    }

    async Task BindData()
    {
        _pageData = await _iApproveAppService.GetFlowPage(_pageRequest);
    }

    void OnRowClick(RowData<WorkFlowDto> row)
    {
        Console.WriteLine($"row {row.Data.FlowName} was clicked.");
    }

    async Task Delete(Guid flowId)
    {
        await _iApproveAppService.Delete(flowId);
        await BindData();
    }

    async Task FlowStart(Guid flowId)
    {
        await _iApproveAppService.FlowStart(flowId);
        await BindData();
    }

}
